/*
  Copyright (c) 2010 Xynilex Project

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
 */

#ifndef _XYNILEX_INPUT_INPUTMANAGER_H_
#define _XYNILEX_INPUT_INPUTMANAGER_H_

// System Headers
#include <map>
#include <stdlib.h>

// OIS Headers
#include "OISMouse.h"
#include "OISKeyboard.h"
#include "OISJoyStick.h"
#include "OISInputManager.h"

// Xynilex Headers
#include "Common/Types.h"
#include "Input/InputCommand.h"
#include "Input/JoyStick.h"
#include "Input/Keyboards.h"
#include "Input/Mice.h"

namespace Xynilex
{
  namespace Input
  {
    class InputManager : public OIS::MouseListener, OIS::JoyStickListener
    {
    public:
      InputManager();
      ~InputManager();

      void initialize(size_t windowHandle, int width, int height);
      void shutdown();

      void update();

      void setWindowExtents(int width, int height);

      void registerCommand(const String& name, InputCommand* handler);
      void unregisterCommand(const String& name);
      InputCommand* getCommandHandler(const String& name) const;

      void bind(const String& triggerName, const String& command);
      void bind(const String& triggerName, InputCommand* command);
      void unbind(const String& triggerName);

    protected:
      bool mInitialized;

      OIS::InputManager* mInputSystem;

      Keyboards* mKeyboards;
      Mice* mMice;
      JoyStick* mJoySticks;

      typedef std::vector<OIS::Object*> InputDeviceList;
      InputDeviceList mInputDevices;

      typedef std::map<String, InputCommand*> InputCommandMap;
      InputCommandMap mInputCommands;

      // trigger name -> command name
      typedef std::map<String, InputCommand*> TriggerCommandMap;
      TriggerCommandMap mBinds;

      typedef std::map<String, bool> TriggerDownMap;
      TriggerDownMap mTriggerDown;

      bool commandExists(const String& name);
      bool triggerCommandExists(const String& trigger);

      float applySensitivity(float raw, const String& name);

      bool mouseMoved(const OIS::MouseEvent &e);
      bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id);
      bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id);

      bool povMoved(const OIS::JoyStickEvent &e, int pov);
      bool axisMoved(const OIS::JoyStickEvent &e, int axis);
      bool sliderMoved(const OIS::JoyStickEvent &e, int sliderID);
      bool buttonPressed(const OIS::JoyStickEvent &e, int button);
      bool buttonReleased(const OIS::JoyStickEvent &e, int button);
    };
  }
}

#endif // _XYNILEX_INPUT_INPUTMANAGER_H_
